<template>
  <XtxDialog :title="`${formData.id?'修改':'添加'}收货地址`" v-model:visible="dialogVisible">
    <div class="xtx-form">
      <div class="xtx-form-item">
        <div class="label">收货人：</div>
        <div class="field">
          <input v-model="formData.receiver" class="input" placeholder="请输入收货人" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">手机号：</div>
        <div class="field">
          <input v-model="formData.contact" class="input" placeholder="请输入手机号" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地区：</div>
        <div class="field">
          <XtxCity @change="changeCity" :fullLocation="formData.fullLocation" placeholder="请选择所在地区" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">详细地址：</div>
        <div class="field">
          <input v-model="formData.address" class="input" placeholder="请输入详细地址" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">邮政编码：</div>
        <div class="field">
          <input v-model="formData.postalCode" class="input" placeholder="请输入邮政编码" />
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地址标签：</div>
        <div class="field">
          <input v-model="formData.addressTags" class="input" placeholder="请输入地址标签，逗号分隔" />
        </div>
      </div>
    </div>
    <template v-slot:footer>
      <XtxButton @click="dialogVisible=false" type="gray" style="margin-right:20px">取消</XtxButton>
      <XtxButton @click="submit" type="primary">确认</XtxButton>
    </template>
  </XtxDialog>
</template>

<script>
import { ref, reactive } from 'vue'
import { addAddress, editAddress } from '@/api/order'
import Message from '@/components/lilbrary/Message'

export default {
  name: 'AddressEdit',
  setup(props, { emit }) {
    const dialogVisible = ref(false)
    // 打开函数
    const open = (address) => {
      if (!address) {
        return Message({ text: '请先添加地址' })
      }
      dialogVisible.value = true
      if (address.id) {
        // 修改地址
        for (const key in address) {
          formData[key] = address[key]
        }
      } else {
        // 添加地址
        for (const key in formData) {
          formData[key] = ''
          if (key === 'isDefault') {
            formData[key] = 0
          }
        }
      }
    }

    // 表单数据
    const formData = reactive({
      receiver: '',
      contact: '',
      provinceCode: '',
      cityCode: '',
      countyCode: '',
      address: '',
      postalCode: '',
      addressTags: '',
      isDefault: 0,
      fullLocation: '',
      id: ''
    })

    const changeCity = (data) => {
      // ref 数据
      data = data.value
      formData.provinceCode = data.provinceCode
      formData.cityCode = data.cityCode
      formData.countyCode = data.countyCode
      formData.fullLocation = data.fullLocation
    }

    // 提交数据
    const submit = async () => {
      // 省略了校验(完善项目加校验)
      if (formData.id) {
        // 修改请求
        if (!formData.receiver || !formData.contact || !formData.fullLocation || !formData.address || !formData.postalCode || !formData.addressTags) {
          return Message({ text: '请填写完整信息' })
        }
        editAddress(formData.id, formData).then(data => {
          Message({ text: '修改收货地址成功', type: 'success' })
        })
      } else {
        // 添加请求
        if (!formData.receiver || !formData.contact || !formData.fullLocation || !formData.address || !formData.postalCode || !formData.addressTags) {
          return Message({ text: '请填写完整信息' })
        }
        const data = await addAddress(formData)
        formData.id = data.result.id
        Message({ type: 'success', text: '添加收货地址成功' })
      }
      dialogVisible.value = false
      emit('on-success', formData)
    }
    return { dialogVisible, open, formData, changeCity, submit }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper) {
    width: 780px;

    .body {
      font-size: 14px;
    }
  }
}

.xtx-form {
  padding: 0;

  .xtx-form-item {
    display: flex;
    align-items: center;
    width: 700px;
    margin: 0 auto;
    padding-bottom: 25px;

    .label {
      width: 180px;
      padding-right: 10px;
      text-align: right;
      color: #999;
    }

    .field {
      width: 320px;
      height: 50px;
      position: relative;
      margin-left: 0;

      .input {
        border: 1px solid #e4e4e4;
        width: 320px;
        height: 50px;
        line-height: 50px;
        padding: 0 10px;
      }
    }
  }

  input {
    outline: none;

    &::placeholder {
      color: #ccc;
    }
  }
}

.xtx-city {
  width: 320px;

  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;

    .placeholder {
      color: #ccc;
    }

    i {
      color: #ccc;
      font-size: 18px;
    }

    .value {
      font-size: 14px;
    }
  }

  :deep(.option) {
    top: 49px;
  }
}
</style>
